/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package adbus.models;

import adbus.libraries.*;
import java.sql.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author rafal
 */
abstract public class Table extends AbstractTableModel {
    protected ResultSet result = null;
    protected LinkedHashMap<String,String> filters = new LinkedHashMap();
    protected LinkedHashMap<String,Object> filtersval = new LinkedHashMap();
    public String basicQuery() {
        return "";
    }
    public void zaladuj() {
        try {
            result = Database.querySelect(basicQuery() +getWhere(),filtersval);
            fireTableDataChanged();
        } catch (SQLException ex) {
            Logger.getLogger(Table.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    protected String getWhere() {
        String where = " WHERE ";
        //String col[] = filters.values().toArray(new String[0]);
        if(filters.size() == 0) return "";
        int i = 0;
        for(Map.Entry<String,String> entry: filters.entrySet()) {
            where+=entry.getValue();
            if(i<filters.size()-1) where+=" AND ";
            i++;
        }
        return where;
    }

    public void resetFilters() {
        filters.clear();
        filtersval.clear();
        zaladuj();
    }
}
